package com.lanhai.javaBean;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.lanhai.enums.AgvState;
import com.lanhai.model.Agv;
import com.mybatis.dao.AgvDao;

public class AgvBean {

	/**
	 * 加载Agv列表
	 */
	public static List<Agv> loadData(SqlSessionFactory sqlSessionFactory){
		List<Agv> agvList = new ArrayList<Agv>();
	    SqlSession sqlSession = sqlSessionFactory.openSession();
	    try{
		    AgvDao agvDao = sqlSession.getMapper(AgvDao.class);
		    List<Map<String, Object>> list = agvDao.selectAll();
		    if(list.size() > 0){
		    	for(Map<String, Object> m:list){
		    		Agv agv = new Agv();
		    		agv.setId((Integer)m.get("id"));
		    		agv.setIpAddress((String)m.get("ipAddress"));
		    		agv.setPort((Integer)m.get("port"));
		    		agv.setWaitParkTimes(new Date());
		    		agv.setState(AgvState.LX.getCode());//离线
		    		agvList.add(agv);
		    	}
		    }
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	sqlSession.close();
	    }
		return agvList;
	}	

	public static List<Map<String, Object>> loadTempData(SqlSessionFactory sqlSessionFactory){
		List<Map<String, Object>> agvList = new ArrayList<Map<String, Object>>();
		SqlSession sqlSession = sqlSessionFactory.openSession();
		try {
			AgvDao agvDao = sqlSession.getMapper(AgvDao.class);
			agvList = agvDao.selectTempData();
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	sqlSession.close();
	    }
		return agvList;
	}	
	
	public static List<Agv> loadData4CVS(String dataPath){
		List<Agv> agvList = new ArrayList<Agv>();
		try {
        	//InputStream is = ChargerBean.class.getClass().getResourceAsStream("/data/agvList.csv"); 
        	//BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        	String filePath = dataPath + "/agvList.csv";
        	BufferedReader reader = new BufferedReader(new FileReader(filePath));//换成你的文件名
            reader.readLine();//第一行信息，为标题信息，不用，如果需要，注释掉
            String line = null; 
            while((line=reader.readLine())!=null){
            	Agv agv = new Agv();
                String item[] = line.split(",");//CSV格式文件为逗号分隔符文件，这里根据逗号切分
                agv.setId(Integer.parseInt(item[0]));//ID
                //agv.setPathItem(Integer.parseInt(item[1]));//线路号
                //agv.setFirstChargerId(Integer.parseInt(item[2]));//对应充电桩ID
                //agv.setChargerNum(Integer.parseInt(item[3]));//对应充电桩号码
                agv.setCtrlFlag(Integer.parseInt(item[4]));
                //agv.setCharged(Integer.parseInt(item[5]));
                agv.setIpAddress(item[6]);
                agv.setPort(Integer.parseInt(item[7]));
                agvList.add(agv);
            } 
            reader.close();
        } catch (Exception e) { 
            e.printStackTrace(); 
        }
        return agvList;
	}
	
	public static List<Agv> LastPointSort(List<Agv> agvList){
        Collections.sort(agvList, new Comparator<Object>() {
            public int compare(Object a, Object b) {
                int one = ((Agv) a).getNumber();
                int two = ((Agv) b).getNumber();
                //return one - two;//升序
                return two - one;//降序
            }
        });	
        return agvList;
	}
	
	
	public static List<Agv> waitParkTimeSort(List<Agv> agvList){
        Collections.sort(agvList, new Comparator<Object>() {
            public int compare(Object a, Object b) {
            	long one = ((Agv) a).getWaitParkTimes().getTime();
                long two = ((Agv) b).getWaitParkTimes().getTime();
                int x = 0;
                if(one > two){
                	x = 1;
                }else if(one < two){
                	x = -1;
                }
                return x;
            }
        });	
        return agvList;
	}
}
